/*
 * Copyright 2016 Wealth Inc. All rights reserved.
 */
 package com.wealth.bank.service.impl;

import java.util.List;
import java.util.Map;

import iaas.enums.StatusEnum;
import iaas.service.impl.BaseServiceImpl;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.wealth.bank.entity.Branch;
import com.wealth.bank.repository.BranchDao;
import com.wealth.bank.service.IBranchService;
import com.wealth.bank.util.BranchDistance;
import com.wealth.platform.entity.Account;
import com.wealth.platform.service.impl.AccountServiceImpl;
import com.wealth.system.entity.Corporation;

/**
 * @author chan
 * @version 1.0
 * @since 1.0
 */
 
@Component
public class BranchServiceImpl extends BaseServiceImpl<Branch> implements IBranchService{
	
	Logger logger = Logger.getLogger(BranchServiceImpl.class);

	
	@Autowired
	private BranchDao branchDao;

	//查询银行网点
	@Override
	public List<Branch> findByCorpIdAndStatus(Long corpId, Integer status) {
		return branchDao.findByCorpIdAndStatus(corpId, status);
	}

	//查询附近银行网点
	@Override
	public List<Branch> getNearbyBranch(Long corpId, Integer status,double longitude,double latitude) {
		
		List<Branch> branchs=branchDao.findByCorpIdAndStatus(corpId, status);
		for (Branch branch : branchs) {
			double distance=BranchDistance.Distance(longitude, latitude, branch.getLongitude(), branch.getLatitude());
			if(distance>1000){
				branchs.remove(branch);
			}
		}
		return branchs;
	}

	//模糊查询网点
	@Override
	public List<Branch> findByCorpIdAndStatusAndAddressLike(Long corpId,
			Integer status, String address) {
		return branchDao.findByCorpIdAndStatusAndAddressLike(corpId, status, "%"+address+"%");
	}
	
	@Override
	public boolean importFile(MultipartFile file,Long corpid) throws Exception {
	
		try {
			/** -------- 读取文件 -------- */
			//第一种方式，根据后缀名使用不同的实现类
			//将excel文件转换为输入流
//			workbook = new HSSFWorkbook();
//			if(file.getName().indexOf(".xlsx")>-1){//2007格式XSSF
//				workbook = new XSSFWorkbook();
//			}

			//第二种方式
			//工作簿
			Workbook workbook = WorkbookFactory.create(file.getInputStream());		
			//工作表
			Sheet sheet =workbook.getSheetAt(0); 					
			//总行
			int rows = sheet.getLastRowNum();																
			logger.info("--> poi read : rows="+rows);
			//遍历行, r=0 对应说明文字
			for (int r = 1; r<=rows; r++) {
				Row row  = sheet.getRow(r);
				if(row==null){
					continue;
				}
				//总列
				int cells = row.getLastCellNum();			
				//账户
				
				
				Branch branch = new Branch();
				//遍历每一列
				for (int col = 0; col<cells; col++) {
					//设置每一列为字符串
					if(row.getCell(col)!=null){
						row.getCell(col).setCellType(Cell.CELL_TYPE_STRING);
					}
				}
				branch.setName(row.getCell(0)==null?"":row.getCell(0).toString());
				branch.setAddress(row.getCell(1)==null?"":row.getCell(1).toString());
				branch.setPcode(row.getCell(2)==null?0:Integer.parseInt(row.getCell(2).getStringCellValue()));
				branch.setTelephone(row.getCell(3)==null?"":row.getCell(3).toString());
				branch.setCorp(new Corporation(corpid));
				branch.setStatus(StatusEnum.NORMAL.getValue());
				branchDao.save(branch);
			}
				workbook.close();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} 
	}

	
	
	
	
	
}
